
#include "Felowria.h"
#include "Network/SocketManager.h"
#include "Base/DateTime.h"
#include "UserSocket.h"
#include "UserAcceptor.h"
#include <vector>
#include "Thread/ThreadMutex.h"
#include "TestService.h"
#include "User.h"
#include "Game/WorkThread.h"
using namespace std;
using namespace Felowria;

#define SERVICE_SIZE	4

class TestServer
{
public:
	TestServer():user_count(0) {}
	~TestServer() {}

public:
	bool Load()
	{
		socket_manager = SocketManager::Instance();
		
		acceptor = new UserAcceptor("0.0.0.0:", 51001);
		
		socket_manager->SpawnSocketThread(1);
		acceptor->SetServer(this);
		acceptor->ExcuteTask();
	
		for(int i = 0;i < SERVICE_SIZE;++i)
			service[i].ExcuteTask();

		return true;
	}

	bool UnLoad()
	{
		return true;
	}

	bool Run_In_Loop()
	{
		while (1)
		{
			cur_time = CurTime();
			
			Wait(0.01);
		}

		return true;
	}

public:
	void AddSocket(Socket *socket)
	{
		User *user = new User((UserSocket*)socket);
		service[user_count++ % SERVICE_SIZE].AddUser(user);
		for(int i =0;i < SERVICE_SIZE;++i)
			service[i].AddAllUser(user);
	}

private:
	SocketManager *socket_manager;
	UserAcceptor *acceptor;
	TestService service[SERVICE_SIZE];
	int32 user_count;
};
